package com.archie.xunji.map.tools;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;

import com.archie.xunji.map.Constans;

import java.io.IOException;

public class PlayerService extends Service {

    private MediaPlayer mediaPlayer;
    private String lastUrl;

    public PlayerService() {

        mediaPlayer = new MediaPlayer();

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        String url = intent.getStringExtra("url");

        if (url != null) {

            if(lastUrl == null) {   //首次播放
                try {
                    mediaPlayer.setDataSource(getApplicationContext(),Uri.parse(url));
                    mediaPlayer.prepare();
                    mediaPlayer.start();

                } catch (IOException e) {
                    e.printStackTrace();
                }

                new SendThread().start();
            }else if(lastUrl.equals(url)){//播放同一个音频，暂停播放
                if(mediaPlayer.isPlaying())
                    mediaPlayer.pause();
                else {
                    mediaPlayer.start();

                    new SendThread().start();
                }

            }else{  //切换音频
                mediaPlayer.reset();

                try {
                    mediaPlayer.setDataSource(getApplicationContext(),Uri.parse(url));
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                new SendThread().start();
            }


            lastUrl = url;
        }


        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if(mediaPlayer.isPlaying()){
            mediaPlayer.stop();
        }
        mediaPlayer.release();
    }

    private class SendThread extends Thread{
        @Override
        public void run() {


            try {
                while (mediaPlayer!=null && mediaPlayer.isPlaying()) {

                    getTime();

                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    private void getTime() throws InterruptedException {
        long totalTime = mediaPlayer.getDuration()/1000;// 获取播放文件的总时长，单位：毫秒
        long currentTime = mediaPlayer.getCurrentPosition()/1000; // 获取当前播放的位置

        // 准备发送进度广播
        Intent intent = new Intent(Constans.BROADCAST_ACTION_UPDATETIME);
        intent.putExtra("totalTime",totalTime); // 总时长
        intent.putExtra("currentTime",currentTime);// 当前进度

        sendBroadcast(intent);// 发送播放的进度广播

        Thread.sleep(200);
    }

}
